package com.example.music_player_app.ui.dashboard;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.fastjson.JSONObject;
import com.example.music_player_app.Bean.SongDetail;
import com.example.music_player_app.R;
import com.example.music_player_app.api.service.playList.PlayListService;
import com.example.music_player_app.databinding.FragmentDashboardBinding;
import com.example.music_player_app.utils.ApiServer;
import com.example.music_player_app.utils.CookieServer;
import com.example.music_player_app.utils.DBHelper;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class DashboardFragment extends Fragment {

    private DashboardViewModel dashboardViewModel;
    private FragmentDashboardBinding binding;
    private ListView listView; // 我的歌单

    private DBHelper dbHelper;
    private SQLiteDatabase db;
    private Cursor cursor;

    private List<SongDetail> list = new ArrayList<>();

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                new ViewModelProvider(this).get(DashboardViewModel.class);

        binding = FragmentDashboardBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        listView = binding.myMusicList;

        initMyMusicList(); //初始化我的歌单

        return root;
    }

    private void initMyMusicList() {
        dbHelper = new DBHelper(getContext(), "musicPlayerDB.db", null, 1);
        db = dbHelper.getWritableDatabase();

        String sql = "select * from myMusic";
        cursor = db.rawQuery(sql, null);
        if(cursor.getCount() > 0) {
            if (cursor.moveToFirst()) {
                do {
                    SongDetail songDetail = new SongDetail();
                    // 遍历Cursor对象，取出数据并打印
                    Long id = cursor.getLong(cursor.getColumnIndex("id"));
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    String singer = cursor.getString(cursor.getColumnIndex("singer"));
                    String songImgUrl = cursor.getString(cursor.getColumnIndex("songImgUrl"));
                    String playUrl = cursor.getString(cursor.getColumnIndex("playUrl"));

                    songDetail.setId(id);
                    songDetail.setName(name);
                    songDetail.setSinger(singer);
                    songDetail.setSongImgUrl(songImgUrl);
                    songDetail.setPlayUrl(playUrl);

                    list.add(songDetail);
                }
                while (cursor.moveToNext());
            }
            TextView textView2 = binding.noMusic;
            if(list.size() <= 0){
                textView2.setVisibility(View.VISIBLE);
            } else {
                textView2.setVisibility(View.INVISIBLE);
            }
            listView.setAdapter(new MyMusicAdapter(list, getActivity()));
        }

    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}